package com.zymail.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zymail.bean.ZyUser;
import com.zymail.mapper.ZyUserMapper;
import com.zymail.service.ZyUserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author Dorado
 * @since 2021-04-12
 */
@Service
public class ZyUserServiceImpl extends ServiceImpl<ZyUserMapper, ZyUser> implements ZyUserService {
    @Autowired
    private ZyUserService userService;

    /**
     * 如果数据库里存在该
     * @param user
     * @return
     */
    public ZyUser login(ZyUser user){
        QueryWrapper<ZyUser> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("openid", user.getOpenid());
        ZyUser res = userService.getOne(queryWrapper);
        System.out.println("res :" + res);
        if(res == null){
            userService.save(user);
            if(user.getFaId() == null) {
                user.setFaId(user.getId());
                userService.updateById(user);
            }
            return user;
        }
        return res;
    }

    @Override
    public List<Integer> getroot(Integer userId) {
        ArrayList<Integer> res = new ArrayList<>();

        Integer nowid = userId;
        for(int i = 0; i < 3; i++){
            ZyUser now = getById(nowid);
            if(now.getFaId().equals(nowid)) break;
            res.add(now.getFaId());
            nowid = now.getFaId();
        }

        return res;
    }
}
